/**
 * Copyright (C) 2007 Mustafa K. Isik - isik@acm.org
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

package com.paxcodi.rmi.calculator;

import java.net.MalformedURLException;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.Naming;

@SuppressWarnings("serial")
public class CalculatorServer extends UnicastRemoteObject implements Calculator {

    public CalculatorServer() throws RemoteException{
	super(); //creates a UniCastRemoteObject on some port and starts it listening for connections
    }

    /**
     * Adds two parameters of type <code>double</code>.
     * 
     * @param firstOperand first summand
     * @param secondOperand second summand
     * @return sum of firstOperand and secondOperand
     */
    public double add(double firstOperand, double secondOperand) {
	double result = firstOperand + secondOperand;
	System.out.println(firstOperand + " + " + secondOperand + " = " + result);
	return result;
    }

    /**
     * Divides parameter <code>firsOperand</code> by <code>secondOperand</code>.
     * 
     * @param firstOperand the dividend
     * @param secondOperand the divisor
     * @return the quotient
     */
    public double divide(double firstOperand, double secondOperand) {
	double result = firstOperand / secondOperand;
	System.out.println(firstOperand + " / " + secondOperand + " = " + result);
	return result;
    }

    /**
     * Multiplies two parameters of type <code>double</code>.
     * 
     * @param firstOperand first factor
     * @param secondOperand second factor
     * @return product of both factors
     */
    public double multiply(double firstOperand, double secondOperand) {
	double result = firstOperand * secondOperand;
	System.out.println(firstOperand + " * " + secondOperand + " = " + result);
	return result;
    }

    /**
     * Subtracts two parameters of type <code>double</code>.
     * 
     * @param firstOperand the minuend
     * @param secondOperand the subtrahend
     * @return difference of firstOperand and secondOperand
     */
    public double subtract(double firstOperand, double secondOperand) {
	double result = firstOperand - secondOperand;
	System.out.println(firstOperand + " - " + secondOperand + " = " + result);
	return result;
    }

    /**
     * Starts the Calculator RMI Server.
     * Performs a <code>Naming.rebind()</code> on a <code>UnicastRemoteObject</code>.
     * Therefore a nondaemon thread is implicitly spawned that keeps the server listening.
     * 
     * @param args default commandline parameter array - unused
     */
    public static void main(String[] args) {
	try {
	    /*
	    //added this security manager code, win as server in vmware would bitch without it 
	    if (System.getSecurityManager() == null) {

		System.setSecurityManager(new RMISecurityManager());
	    }
	    */
	    Calculator calc = new CalculatorServer();
	    Naming.rebind("calculator", calc);
	} catch (RemoteException e) {
	    System.err.println("Exception in CalculatorServer.main " + e);
	} catch (MalformedURLException e) {
	    System.err.println("Malformed URL Exception " + e);
	}
    }

}
